/*
 * Function test of qtsm_lib_api using glib framework
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include "gtest_qtsm_lib_comm.h"

static comm_req g_qtsm_get_desc_reqs[] = {
    {
        .resmsg = {
            .type = DESCRIBE_QTSM,
            .qtsm_message_res_u = {
                .describe_qtsm = {
                    .version_major = 1,
                    .version_minor = 0,
                    .version_patch = 0,
                    .module_id = {"enc-001"},
                    .max_pcrs = 32,
                    .locked_pcrs_num = 17,
                    .locked_pcrs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 31},
                    .digest = 1
                }
            }
        }
    }
};

void gtest_qtsm_get_describe(const void *qtsm_dev_fd)
{
    struct describe_qtsm cur_qtsm;
    int rc;
    unsigned int i;

    for (i = 0; i < ARRAY_SIZE(g_qtsm_get_desc_reqs); i++) {
        memset(&cur_qtsm, 0, sizeof(cur_qtsm));

        rc = qtsm_get_describe(*(int *)qtsm_dev_fd, &cur_qtsm);

        if (g_qtsm_get_desc_reqs[i].resmsg.type == ERROR_CODE) {
            /* check error code */
            g_assert_cmpint(rc, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.error_num);
        } else {
            /* check ret value */
            g_assert_cmpint(rc, ==, NO_ERROR);
            /* check qtsm val */
            /* version_major */
            g_assert_cmpint(cur_qtsm.version_major, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.version_major);
            /* version_minor */
            g_assert_cmpint(cur_qtsm.version_minor, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.version_minor);
            /* version_patch */
            g_assert_cmpint(cur_qtsm.version_patch, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.version_patch);
            /* module_id */
            g_assert_cmpmem(cur_qtsm.module_id, strlen(cur_qtsm.module_id),
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.module_id,
                strlen(g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.module_id));
            /* max_pcrs */
            g_assert_cmpint(cur_qtsm.max_pcrs, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.max_pcrs);
            /* locked_pcrs_num */
            g_assert_cmpint(cur_qtsm.locked_pcrs_num, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.locked_pcrs_num);
            /* locked_pcrs */
            g_assert_cmpmem(cur_qtsm.locked_pcrs, sizeof(uint32_t) * cur_qtsm.locked_pcrs_num,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.locked_pcrs,
                sizeof(uint32_t) * g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.locked_pcrs_num);
            /* digest */
            g_assert_cmpint(cur_qtsm.digest, ==,
                g_qtsm_get_desc_reqs[i].resmsg.qtsm_message_res_u.describe_qtsm.digest);
        }
    }
}
